<script setup lang="ts">
import { loginStore } from '../../store/loginStore'
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import user from '../../api/user';
import { userModel } from '../../model/userModel';
import { ElMessage } from 'element-plus'


const loginStoreImpl = loginStore();

const ruleFormRef = ref<FormInstance>()

const checkNickName = (rule: any, value: any, callback: any) => {
  if (ruleForm.nickName == '') {
    return callback(new Error('请输入昵称'))
  } else {
    callback();
  }
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (ruleForm.checkPass !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPass', () => null)
    }
    callback()
  }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请再一次输入密码'))
  } else if (value !== ruleForm.pass) {
    callback(new Error("两次密码不相同"))
  } else {
    callback()
  }
}
const validateUsername = (rule: any, value: any, callback: any) => {
  if (ruleForm.username == '') {
    callback(new Error('请输入用户名'))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  username: '',
  pass: '',
  checkPass: '',
  nickName: '',
})

const rules = reactive<FormRules>({
  pass: [{ validator: validatePass, trigger: 'blur' }],
  checkPass: [{ validator: validatePass2, trigger: 'blur' }],
  age: [{ validator: checkNickName, trigger: 'blur' }],
  usn: [{ validator: validateUsername , trigger: 'blur' }]
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      let userBean: userModel = {
        username: ruleForm.username,
        password: ruleForm.pass,
        nickName: ruleForm.nickName
      }
      user.insert(userBean)
        .then(res => {
            if (res.data.code == 200) {
                ElMessage({
                    message: '注册成功',
                    type: 'success'
                })
                loginStoreImpl.registerVis = false;
                setTimeout(() => { loginStoreImpl.visable = true; } , 1000)
            }
        })
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

</script>

<template>
  <el-dialog v-model="loginStoreImpl.registerVis" title="注册" width="30%">
    <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    status-icon
    :rules="rules"
    label-width="120px"
    class="demo-ruleForm"
  >
    <el-form-item label="用户名" prop="usn">
      <el-input v-model="ruleForm.username" autocomplete="off" />
    </el-form-item>
    <el-form-item label="密码" prop="pass">
      <el-input v-model="ruleForm.pass" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item label="再一次输入密码" prop="checkPass">
      <el-input
        v-model="ruleForm.checkPass"
        type="password"
        autocomplete="off"
      />
    </el-form-item>
    <el-form-item label="昵称" prop="age">
      <el-input v-model.number="ruleForm.nickName" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)"
        >注册</el-button
      >
      <el-button @click="resetForm(ruleFormRef)">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>
</template>

<style scoped>

</style>